import requests
from typing import Optional, Dict, Any


class APIClient:
    """API客户端，处理所有HTTP请求"""

    def __init__(self, headers: Optional[Dict[str, str]] = None):
        self.session = requests.Session()
        self.headers = headers or {}

    def update_headers(self, headers: Dict[str, str]):
        """更新请求头"""
        self.headers.update(headers)

    def get(self, url: str, params: Optional[Dict[str, Any]] = None,
            headers: Optional[Dict[str, str]] = None, allow_redirects: bool = True) -> requests.Response:
        """发送GET请求"""
        request_headers = headers or self.headers
        return self.session.get(url, params=params, headers=request_headers, allow_redirects=allow_redirects)

    def post(self, url: str, json_data: Optional[Dict[str, Any]] = None,
             headers: Optional[Dict[str, str]] = None) -> requests.Response:
        """发送POST请求"""
        request_headers = headers or self.headers
        return self.session.post(url, json=json_data, headers=request_headers)

    def request(self, method: str, url: str, json_data: Optional[Dict[str, Any]] = None,
                params: Optional[Dict[str, Any]] = None, headers: Optional[Dict[str, str]] = None,
                stream: bool = False, allow_redirects: bool = True) -> requests.Response:
        """发送通用请求"""
        request_headers = headers or self.headers
        return requests.request(
            method=method,
            url=url,
            json=json_data,
            params=params,
            headers=request_headers,
            stream=stream,
            allow_redirects=allow_redirects
        )
